﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MixScriptWeb.control;
using MixScriptWeb.Share;
using MixScript.BLL;
using MixScriptWeb.AppCode.gjips;
using MixScript.BLL.MM;
using MixScript.BLL.SD;
namespace MixScriptWeb.Jideco.SD
{
    public partial class fInvoice : cCommonWeb2, IMIxCommon
    {
        private static DataTable dt;
        
        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {

                mn = (MainMenu2)this.Master;
                hideStatusBar();
                if (!Page.IsPostBack)
                {
                    string[] sheadertext = new string[] { "Delivery No", "Customer", "QTY", "Unit"};
                    string[] sdatavalue = new string[] { "delno","cuid","totqty","totuom"};

                    addFindClickAttribute(this, new MixScriptTB[] { txtSupplier }, new string[] { "SRCCUST" });
                    //txtSupp.Tbx.Attributes.Add("readonly", "readonly");
                    initGvColumn(gvdetail, sheadertext, sdatavalue);
                    MultiView1.SetActiveView(vwFilter);
                    addRead(new MixScriptTB[] { txtInvNo });


                //    txtInvNo.Text = "9999/INV/JOID/IV/12";
                }


            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        #region IMIxCommon Members

        public bool isvalid()
        {
            throw new NotImplementedException();
        }

        #endregion

        protected void btnNext_Click(object sender, EventArgs e)
        {
            try
            {
                dt= new DataTable();
                string sout = "";
                if (txtDate.Text != string.Empty)
                {
                //   if (new cReferences().getInquiry(ref dt, new string[] { "", "", "", "", txtSupplier.Text, "", "", txtDate.Text.Substring(3, 2) + "-" + txtDate.Text.Substring(0, 2) + "-" + txtDate.Text.Substring(6, 4) }, "tb_dnmst", out sout))
                    if (new cReferences().getInquirySQL(ref dt,"select * from tb_dnmst where delno not in (select delno from tb_invdtl)","tb_dnmst", out sout))
                    {
                        gvdetail.DataSource = dt;
                        gvdetail.DataBind();
                        MultiView1.SetActiveView(vwGR);
                    }
                    else
                    {
                        ShowStatusBar("Records Not Found");

                    }
                }
                else
                {

                    ShowStatusBar("Date Must be filled");

                }

                
                
              

            }
            catch (Exception ex)
            {

                throw;
            }
        }

        protected void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                bool b = ((CheckBox)sender).Checked;


                foreach (GridViewRow gr in gvdetail.Rows)
                {
                    CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbitem");
                    cb.Checked = b;
                }

            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void gvdetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int[] i = new int[] { };
               int  isel = 0;
                List<int> li = new List<int>();
                int c = 0;
                int r = 0;

                foreach (GridViewRow gr in gvdetail.Rows)
                {
                    if (!((CheckBox)gr.Cells[0].FindControl("cbitem")).Checked)
                    {
                        li.Add(c);
                        dt.Rows.RemoveAt(c - r);
                        r += 1;
                    }
                    else
                    {
                        isel += 1;
                    }

                    c += 1;
                }
                if (isel > 0)
                {
                    dt.AcceptChanges();
                    string s = "";
                    if (new cInvoice().saveinvoice(dt, out s))
                    {

                        Session["sinv"] = s;

                        Response.Write("<script>");
                        Response.Write("window.open('../print/rptid-5,invno-" +"1"+ ",dl.html', '_blank');");
                        //Response.Write("alert('sdf');");

                        Response.Write("</script>");
                        ShowStatusBar("Invoice Created", msgType.msgSuccess);
                        //txtInvNo.Text = s;
                        MultiView1.SetActiveView(vwFilter);
                    }
                    else
                    {
                        ShowStatusBar(s);

                    }
                }
                else
                {
                    ShowStatusBar("Please Select Delivery Number");
                }

                
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void btnCreateDN_Click(object sender, EventArgs e)
        {

        }

      

       
    }
}